package org.mfoyou.agent.center.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.mfoyou.agent.center.dao.inf.HuawuAddressMapper;
import org.mfoyou.agent.center.dao.inf.HuawuAgentInfoMapper;
import org.mfoyou.agent.center.dao.inf.HuawuAgentStoreSettingMapper;
import org.mfoyou.agent.center.dao.inf.HuawuAgentWxInfoMapper;
import org.mfoyou.agent.center.dao.inf.HuawuAlipayMapper;
import org.mfoyou.agent.center.dao.inf.HuawuEvaluateMapper;
import org.mfoyou.agent.center.dao.inf.HuawuEvaluateTagMapper;
import org.mfoyou.agent.center.dao.inf.HuawuFileMapper;
import org.mfoyou.agent.center.dao.inf.HuawuGoodsEvaluateMapper;
import org.mfoyou.agent.center.dao.inf.HuawuGoodsEvaluateTagMapper;
import org.mfoyou.agent.center.dao.inf.HuawuNoticeMapper;
import org.mfoyou.agent.center.dao.inf.HuawuServiceTagMapper;
import org.mfoyou.agent.center.dao.inf.HuawuSystemMapper;
import org.mfoyou.agent.center.dao.inf.HuawuUserMapper;
import org.mfoyou.agent.center.dao.inf.HuawuWxPushModelMapper;
import org.mfoyou.agent.center.dao.inf.MfoyouThirdPartAuditMapper;
import org.mfoyou.agent.center.dao.mfoyou.inf.MfoyouGdCityMapper;
import org.mfoyou.agent.center.dao.mfoyou.inf.MfoyouUserStatisticsMapper;
import org.mfoyou.agent.common.dao.domain.HuawuAddress;
import org.mfoyou.agent.common.dao.domain.HuawuAddressExample;
import org.mfoyou.agent.common.dao.domain.HuawuAddressExample.Criteria;
import org.mfoyou.agent.common.dao.domain.HuawuAgentInfo;
import org.mfoyou.agent.common.dao.domain.HuawuAgentStoreSetting;
import org.mfoyou.agent.common.dao.domain.HuawuAgentWxInfo;
import org.mfoyou.agent.common.dao.domain.HuawuAgentWxInfoExample;
import org.mfoyou.agent.common.dao.domain.HuawuAlipay;
import org.mfoyou.agent.common.dao.domain.HuawuEvaluate;
import org.mfoyou.agent.common.dao.domain.HuawuEvaluateExample;
import org.mfoyou.agent.common.dao.domain.HuawuEvaluateTag;
import org.mfoyou.agent.common.dao.domain.HuawuEvaluateTagExample;
import org.mfoyou.agent.common.dao.domain.HuawuFile;
import org.mfoyou.agent.common.dao.domain.HuawuFileExample;
import org.mfoyou.agent.common.dao.domain.HuawuGoodsEvaluate;
import org.mfoyou.agent.common.dao.domain.HuawuGoodsEvaluateExample;
import org.mfoyou.agent.common.dao.domain.HuawuGoodsEvaluateTag;
import org.mfoyou.agent.common.dao.domain.HuawuGoodsEvaluateTagExample;
import org.mfoyou.agent.common.dao.domain.HuawuNotice;
import org.mfoyou.agent.common.dao.domain.HuawuNoticeExample;
import org.mfoyou.agent.common.dao.domain.HuawuServiceTag;
import org.mfoyou.agent.common.dao.domain.HuawuServiceTagExample;
import org.mfoyou.agent.common.dao.domain.HuawuUser;
import org.mfoyou.agent.common.dao.domain.HuawuUserExample;
import org.mfoyou.agent.common.dao.domain.HuawuWxPushModel;
import org.mfoyou.agent.common.dao.domain.HuawuWxPushModelExample;
import org.mfoyou.agent.common.dao.domain.MfoyouThirdPartAudit;
import org.mfoyou.agent.common.dao.domain.MfoyouThirdPartAuditExample;
import org.mfoyou.agent.common.dao.mfoyou.domain.MfoyouGdCity;
import org.mfoyou.agent.common.dao.mfoyou.domain.MfoyouGdCityExample;
import org.mfoyou.agent.common.dao.mfoyou.domain.MfoyouUserStatistics;
import org.mfoyou.agent.common.dao.mfoyou.domain.MfoyouUserStatisticsExample;
import org.mfoyou.agent.common.entity.XcxRet;
import org.mfoyou.agent.common.inf.ICommon;
import org.mfoyou.agent.utils.common.ArrayListObject;
import org.mfoyou.agent.utils.sql.MybatisGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class CommonService implements ICommon {

	@Autowired
	HuawuAddressMapper huawuAddressMapper;
	@Autowired
	HuawuAgentWxInfoMapper huawuAgentWxInfoMapper;
	@Autowired
	HuawuFileMapper huawuFileMapper;
	@Autowired
	HuawuServiceTagMapper huawuServiceTagMapper;
	@Autowired
	MfoyouGdCityMapper mfoyouGdCityMapper;
	@Autowired
	HuawuSystemMapper huawuSystemMapper;
	@Autowired
	MfoyouUserStatisticsMapper mfoyouUserStatisticsMapper;
	@Autowired
	MfoyouThirdPartAuditMapper mfoyouThirdPartAuditMapper;
	@Autowired
	HuawuAgentInfoMapper huawuAgentInfoMapper;
	@Autowired
	HuawuAgentStoreSettingMapper huawuAgentStoreSettingMapper;
	@Autowired
	HuawuAlipayMapper huawuAlipayMapper;
	@Autowired
	HuawuWxPushModelMapper huawuWxPushModelMapper;
	@Autowired
	HuawuUserMapper huawuUserMapper;
	@Autowired
	HuawuEvaluateTagMapper  huawuEvaluateTagMapper;
	@Autowired
	HuawuEvaluateMapper 	huawuEvaluateMapper;
	@Autowired
	HuawuGoodsEvaluateMapper huawuGoodsEvaluateMapper;
	@Autowired
	HuawuGoodsEvaluateTagMapper huawuGoodsEvaluateTagMapper;
	@Autowired
	HuawuNoticeMapper huawuNoticeMapper;
	@Override
	public int addMfoyouAddress(HuawuAddress mfoyouAddress) {
		Integer userId = mfoyouAddress.getUserId();
		if (userId == null)
			return -1;
		HuawuAddressExample mfoyouAddressExample = new HuawuAddressExample();
		mfoyouAddressExample.or().andAddressIsDelEqualTo(0).andUserIdEqualTo(userId);
		Integer count = huawuAddressMapper.countByExample(mfoyouAddressExample);
		if (count == 0) {
			mfoyouAddress.setAddressDefalut(1);
		}
		mfoyouAddress.setAddressIsDel(0);
		mfoyouAddress.setAddressId(null);
		int ret = huawuAddressMapper.insertSelective(mfoyouAddress);
		return ret;
	}

	@Override
	public Boolean modifyMfoyouAddress(HuawuAddress mfoyouAddress) {
		if (mfoyouAddress.getAddressId() == null) {
			return false;
		}
		if (mfoyouAddress.getUserId() == null) {
			return false;
		}
		HuawuAddressExample mfoyouAddressExample = new HuawuAddressExample();
		Criteria andUserIdEqualTo = mfoyouAddressExample.or().andAddressIsDelEqualTo(0)
				.andUserIdEqualTo(mfoyouAddress.getUserId());
		if (mfoyouAddress.getAddressDefalut() != null && mfoyouAddress.getAddressDefalut() == 1) {
			HuawuAddress temp = new HuawuAddress();
			temp.setAddressDefalut(0);
			huawuAddressMapper.updateByExampleSelective(temp, mfoyouAddressExample);
		}
		andUserIdEqualTo.andAddressIdEqualTo(mfoyouAddress.getAddressId());
		return huawuAddressMapper.updateByExampleSelective(mfoyouAddress, mfoyouAddressExample) == 1;
	}

	@Override
	public List<HuawuAddress> getUserMfoyouAddress(Integer userId, Integer addressId) {
		HuawuAddressExample mfoyouAddressExample = new HuawuAddressExample();
		HuawuAddressExample.Criteria criteria = mfoyouAddressExample.or().andAddressIsDelEqualTo(0);
		if (addressId != null) {
			criteria.andAddressIdEqualTo(addressId);
		}
		if (userId != null) {
			criteria.andUserIdEqualTo(userId);
		}
		criteria.andAddressIsDelNotEqualTo(1);
		mfoyouAddressExample.setOrderByClause(" address_defalut desc,address_id desc");
		return huawuAddressMapper.selectByExample(mfoyouAddressExample);
	}

	@Override
	public HuawuAgentWxInfo getHuawuWxInfo(Integer agentId, Integer appid) {
		HuawuAgentWxInfoExample huawuAgentWxInfoExample = new HuawuAgentWxInfoExample();
		huawuAgentWxInfoExample.or().andAgentIdEqualTo(agentId).andXcxTypeEqualTo(appid);
		return MybatisGenerator.getOneValue(huawuAgentWxInfoMapper.selectByExample(huawuAgentWxInfoExample));
	}

	@Override
	public HuawuAgentWxInfo getXcxInfoByAppid(String appid) {
		return huawuAgentWxInfoMapper.selectByPrimaryKey(appid);
	}

	@Override
	public int insertFile(HuawuFile fi) {
		return huawuFileMapper.insertSelective(fi);
	}

	@Override
	public HuawuFile getFileInfo(int fileId) {
		return huawuFileMapper.selectByPrimaryKey(fileId);
	}

	@Override
	public List<HuawuFile> getFileInfos(List<Integer> fileIds) {
		HuawuFileExample fileInfoExample = new HuawuFileExample();
		fileInfoExample.or().andFileIdIn(fileIds);
		List<HuawuFile> list = null;
		list = huawuFileMapper.selectByExample(fileInfoExample);
		return list;
	}

	@Override
	public List<HuawuServiceTag> getServiceTags(Integer tageType) {
		HuawuServiceTagExample mfoyouServiceTagExample = new HuawuServiceTagExample();
		mfoyouServiceTagExample.or().andServiceTagTypeEqualTo(tageType);
		return huawuServiceTagMapper.selectByExample(mfoyouServiceTagExample);
	}

	@Override
	public List<MfoyouGdCity> getAll() {
		return mfoyouGdCityMapper.selectByExample(new MfoyouGdCityExample());
	}

	@Override
	public List<MfoyouGdCity> provinceList() {
		MfoyouGdCityExample example = new MfoyouGdCityExample();
		example.or().andCityLeveEqualTo(1).andCityVisbleEqualTo(1);
		return mfoyouGdCityMapper.selectByExample(example);
	}

	@Override
	public List<MfoyouGdCity> cityList(Integer provinceId) {
		MfoyouGdCityExample example = new MfoyouGdCityExample();
		example.or().andCityLeveEqualTo(2).andCityParentAdcodeEqualTo(provinceId).andCityVisbleEqualTo(1);
		return mfoyouGdCityMapper.selectByExample(example);
	}

	@Override
	public List<MfoyouGdCity> countyList(Integer cityId) {
		MfoyouGdCityExample example = new MfoyouGdCityExample();
		example.or().andCityLeveEqualTo(3).andCityParentAdcodeEqualTo(cityId).andCityVisbleEqualTo(1);
		return mfoyouGdCityMapper.selectByExample(example);
	}

	@Override
	public List<MfoyouGdCity> searchCity(String key) {
		MfoyouGdCityExample example = new MfoyouGdCityExample();
		example.or().andCityNameLike("%" + key + "%").andCityVisbleEqualTo(1);
		example.setLimitStart(0);
		example.setLimitEnd(10);
		return mfoyouGdCityMapper.selectByExample(example);
	}

	@Override
	public List<MfoyouGdCity> allCityList() {
		MfoyouGdCityExample example = new MfoyouGdCityExample();
		example.or().andCityLeveEqualTo(2).andCityVisbleEqualTo(1);
		example.setOrderByClause(" city_PY");
		return mfoyouGdCityMapper.selectByExample(example);
	}

	@Override
	public List<MfoyouGdCity> cityListEx(String province) {
		MfoyouGdCityExample example = new MfoyouGdCityExample();
		example.or().andCityLeveEqualTo(2).andCityParentEqualTo(province).andCityVisbleEqualTo(1);
		return mfoyouGdCityMapper.selectByExample(example);
	}

	@Override
	public List<MfoyouGdCity> countyListEx(String city) {
		MfoyouGdCityExample example = new MfoyouGdCityExample();
		example.or().andCityLeveEqualTo(3).andCityParentEqualTo(city).andCityVisbleEqualTo(1);
		return mfoyouGdCityMapper.selectByExample(example);
	}

	@Override
	public List<HuawuFile> getFiles(Integer currentUserId, Integer model) {
		HuawuFileExample mfoyouFileExample = new HuawuFileExample();
		mfoyouFileExample.or().andUserIdEqualTo(currentUserId).andFileTypeLike(model);
		return huawuFileMapper.selectByExample(mfoyouFileExample);
	}

	@Override
	public int getCountVisitUser(String start, String end, Integer storeid) {
		String sql = "select ifnull(sum(user_count),0) totalUser from mfoyou_user_statistics where store_id = "
				+ storeid + " and day_id between " + start + " and " + end;
		return Integer.parseInt(huawuSystemMapper.select_item(sql).get("totalUser").toString());
	}

	@Override
	public int insertUserStatistics(MfoyouUserStatistics mfoyouUserStatistics) {
		return mfoyouUserStatisticsMapper.insertSelective(mfoyouUserStatistics);
	}

	@Override
	public MfoyouUserStatistics getMfoyouUserStatic(MfoyouUserStatistics mfoyouUserStatistics) {
		MfoyouUserStatisticsExample mfoyouUserStatisticsExample = new MfoyouUserStatisticsExample();
		MybatisGenerator.makeExample(mfoyouUserStatistics, mfoyouUserStatisticsExample.or());
		List<MfoyouUserStatistics> selectByExample = mfoyouUserStatisticsMapper
				.selectByExample(mfoyouUserStatisticsExample);
		return selectByExample == null || selectByExample.size() == 0 ? null : selectByExample.get(0);
	}

	@Override
	public int[] getUserStatiscs(Integer storeId, Date startTime, Date endTime) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
		String startTimeFormatResult = simpleDateFormat.format(startTime);
		String endTimeFormatResult = simpleDateFormat.format(endTime);
		String sql = "select ifnull(sum(visit_count),0) visitor,ifnull(sum(user_count),0) user from mfoyou_user_statistics where store_id"
				+ "= " + storeId + " and day_id between " + startTimeFormatResult + " and " + endTimeFormatResult;
		Map<String, Object> select_item = huawuSystemMapper.select_item(sql);
		return new int[] { Integer.parseInt(select_item.get("visitor").toString()),
				Integer.parseInt(select_item.get("user").toString()) };
	}
	@Override
	public List<MfoyouThirdPartAudit> getThirdPartAudit(MfoyouThirdPartAudit mfoyouThirdPartAudit) {
		MfoyouThirdPartAuditExample mfoyouThirdPartAuditExample = new MfoyouThirdPartAuditExample();
		org.mfoyou.agent.common.dao.domain.MfoyouThirdPartAuditExample.Criteria andAppidEqualTo = mfoyouThirdPartAuditExample.or();
		andAppidEqualTo.andAppidEqualTo(mfoyouThirdPartAudit.getAppid());
		MybatisGenerator.makeExample(mfoyouThirdPartAudit, andAppidEqualTo);
		return mfoyouThirdPartAuditMapper.selectByExample(mfoyouThirdPartAuditExample);
	}

	@Override
	public int addHuawuAgentInfo(HuawuAgentInfo huawuAgentInfo) {
		HuawuAgentInfo selectByPrimaryKey = huawuAgentInfoMapper.selectByPrimaryKey(huawuAgentInfo.getAgentId());
		if(selectByPrimaryKey!=null){
			huawuAgentInfoMapper.updateByPrimaryKeySelective(selectByPrimaryKey);
		}
		return huawuAgentInfoMapper.insertSelective(huawuAgentInfo);
	}

	@Override
	public int addAgentStoreSetting(HuawuAgentStoreSetting huawuAgentStoreSetting) {
		HuawuAgentStoreSetting selectByPrimaryKey = huawuAgentStoreSettingMapper.selectByPrimaryKey(huawuAgentStoreSetting.getAgentId());
		if(selectByPrimaryKey!=null){
			return huawuAgentStoreSettingMapper.updateByPrimaryKeySelective(huawuAgentStoreSetting);
		}
		return huawuAgentStoreSettingMapper.insertSelective(huawuAgentStoreSetting);
	}

	@Override
	public int addWxInfos(List<HuawuAgentWxInfo> list) {
		for(int i=0;i<list.size();i++){
			HuawuAgentWxInfo huawuAgentWxInfo = list.get(i);
			HuawuAgentWxInfoExample huawuAgentInfoExample = new HuawuAgentWxInfoExample();
			huawuAgentInfoExample.or().andAgentIdEqualTo(huawuAgentWxInfo.getAgentId())
						.andXcxTypeEqualTo(huawuAgentWxInfo.getXcxType());
			List<HuawuAgentWxInfo> selectByExample = huawuAgentWxInfoMapper.selectByExample(huawuAgentInfoExample);
			if(selectByExample==null || selectByExample.size()==0){
				huawuAgentWxInfoMapper.insertSelective(huawuAgentWxInfo);
				continue;
			}
			huawuAgentWxInfoMapper.updateByPrimaryKeySelective(huawuAgentWxInfo);
		}
		return 0;
	}

	@Override
	public int addAlipay(HuawuAlipay huawuAlipay) {
		Integer agentId = huawuAlipay.getAgentId();
		HuawuAlipay selectByPrimaryKey = huawuAlipayMapper.selectByPrimaryKey(agentId);
		if(selectByPrimaryKey==null){
			return huawuAlipayMapper.insertSelective(huawuAlipay);
		}else{
			return huawuAlipayMapper.updateByPrimaryKeySelective(huawuAlipay);
		}
	}

	@Override
	public int addPushModel(List<HuawuWxPushModel> pushlist) {
		for(int i = 0;i<pushlist.size();i++){
			HuawuWxPushModel huawuWxPushModel = pushlist.get(i);
			HuawuWxPushModelExample huawuWxPushModelExample = new HuawuWxPushModelExample();
			huawuWxPushModelExample.or().andAgentIdEqualTo(huawuWxPushModel.getAgentId())
				.andModelTypeEqualTo(huawuWxPushModel.getModelType());
			huawuWxPushModelMapper.deleteByExample(huawuWxPushModelExample);
			huawuWxPushModelMapper.insertSelective(pushlist.get(i));
		}
		return 0;
	}

	@Override
	public int modifyAgentInfo(HuawuAgentInfo huawuAgentInfo) {
		return huawuAgentInfoMapper.updateByPrimaryKeySelective(huawuAgentInfo);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackForClassName = "Exception")
	public ArrayListObject<Map<String, Object>> getAgentList(Integer pageId, Integer pageSize,String key) {
		Integer pageStart = 0;
//		Integer pageEnd = 10;
		if(pageId!=null && pageSize!=null){
			pageStart = (pageId-1)*pageSize;
//			pageEnd = pageId * pageSize;
		}
		String sql = "select user_id,agent_name,agent_end_time,user_phone,user_type,remark  from "
				+ "(select agent_id user_id,agent_name,agent_end_time,remark  from huawu_agent_info ) "
				+ "a inner join (select user_id,user_nick_name,user_phone,user_type  from huawu_user ";
				if(key!=null){
					sql+="where user_nick_name like '%"+key+"%'  or user_phone like '%"+key+"%' ";
				}
				sql+=  ") b using(user_id)  limit "
				+pageStart+","+pageSize;
		System.out.println(sql);
		List<Map<String, Object>> select_list = huawuSystemMapper.select_list(sql);
		int count = 0;
		if(key!=null){
			count = huawuSystemMapper.selectCount();
		}else{
			sql =  "select  count(1) nu from huawu_agent_info";
			count = Integer.parseInt(huawuSystemMapper.select_item(sql).get("nu").toString());
		}
		return ArrayListObject.bliudArrayList(select_list, pageId, pageSize, count);
	}

	@Override
	public Map<String,Object> getAgentByAgnetId(Integer agentId) {
		Map<String,Object> map = new HashMap<>();
		HuawuUser huawuUser = huawuUserMapper.selectByPrimaryKey(agentId);
		map.put("userInfo", huawuUser);
		HuawuAgentInfo huawuAgentInfo = huawuAgentInfoMapper.selectByPrimaryKey(agentId);
		map.put("agentInfo", huawuAgentInfo);
		return map;
	}

	@Override
	public Map<String, Object> getAgentWxInfoByAgnetId(Integer agentId) {
		Map<String,Object> map = new HashMap<>();
		HuawuAgentWxInfoExample huawuAgentWxInfoExample = new HuawuAgentWxInfoExample();
		huawuAgentWxInfoExample.or().andAgentIdEqualTo(agentId);
		List<HuawuAgentWxInfo> wxInfos = huawuAgentWxInfoMapper.selectByExample(huawuAgentWxInfoExample);
		map.put("wxInfos",wxInfos );
		HuawuAlipay alipay = huawuAlipayMapper.selectByPrimaryKey(agentId);
		map.put("aliPay", alipay);
		HuawuWxPushModelExample huawuWxPushModelExample = new HuawuWxPushModelExample();
		huawuWxPushModelExample.or().andAgentIdEqualTo(agentId);
		List<HuawuWxPushModel> models = huawuWxPushModelMapper.selectByExample(huawuWxPushModelExample);
		map.put("models", models);
		return map;
	}

	@Override
	public int updateHuawuAgentWxInfoByAgentId(HuawuAgentWxInfo huawuAgentWxInfo, Integer agentId) {
		HuawuAgentWxInfoExample huawuAgentWxInfoExample = new HuawuAgentWxInfoExample();
		huawuAgentWxInfoExample.or().andAgentIdEqualTo(agentId);
		return huawuAgentWxInfoMapper.updateByExampleSelective(huawuAgentWxInfo, huawuAgentWxInfoExample);
	}

	@Override
	public List<HuawuEvaluateTag> getTagList(Integer score) {
		HuawuEvaluateTagExample huawuEvaluateTagExample = new HuawuEvaluateTagExample();
		huawuEvaluateTagExample.or().andEvaluateTagScoreEqualTo(score);
		return huawuEvaluateTagMapper.selectByExample(huawuEvaluateTagExample);
	}

	@Override
	public int addHuawuEvaluate(HuawuEvaluate huawuEvaluate) {
		int insertSelective = huawuEvaluateMapper.insertSelective(huawuEvaluate);
		if(insertSelective==1 && huawuEvaluate.getWorkerId()!=null && huawuEvaluate.getEvaluateStar()!=null){
			//更新星级别
			String sql = "update huawu_worker set worker_star = (worker_star*(worker_order_count-1)+"+
					huawuEvaluate.getEvaluateStar()
			+")/worker_order_count where worker_order_count > 0 and user_id ="+huawuEvaluate.getWorkerId() ;
			huawuSystemMapper.update_item(sql);
		}
		return insertSelective;
	}

	@Override
	public List<HuawuGoodsEvaluateTag> getgoodsTagList(Integer score) {
		HuawuGoodsEvaluateTagExample huawuEvaluateTagExample = new HuawuGoodsEvaluateTagExample();
		huawuEvaluateTagExample.or().andEvaluateTagScoreEqualTo(score);
		return huawuGoodsEvaluateTagMapper.selectByExample(huawuEvaluateTagExample);
	}

	@Override
	public int addHuawuGoodsEvaluate(HuawuGoodsEvaluate huawuEvaluate) {
		int insertSelective = huawuGoodsEvaluateMapper.insertSelective(huawuEvaluate);
		if(insertSelective==1 && huawuEvaluate.getWorkerId()!=null && huawuEvaluate.getEvaluateStar()!=null){
			//更新星级别
			String sql = "update huawu_store_info set store_total_score = (store_total_score*(store_total_order_count-1)+"+
					huawuEvaluate.getEvaluateStar()
			+")/store_total_order_count where store_total_order_count > 0 and store_id ="+huawuEvaluate.getWorkerId() ;
			huawuSystemMapper.update_item(sql);
		}
		return insertSelective;
	}

	@Override
	public int increaseAppid(String appid,Integer modelId,String version) {
		MfoyouThirdPartAudit selectByPrimaryKey = mfoyouThirdPartAuditMapper.selectByPrimaryKey(appid);
		MfoyouThirdPartAudit mfoyouThirdPartAudit = new MfoyouThirdPartAudit();
		mfoyouThirdPartAudit.setCurrentModelId(modelId);
		mfoyouThirdPartAudit.setAppid(appid);
		mfoyouThirdPartAudit.setVersionId(version);
		if(selectByPrimaryKey==null){
			mfoyouThirdPartAudit.setState(0);
			return mfoyouThirdPartAuditMapper.insertSelective(mfoyouThirdPartAudit);
		}
		return mfoyouThirdPartAuditMapper.updateByPrimaryKeySelective(mfoyouThirdPartAudit);
	}

	@Override
	public HashMap<String, Object> getModelId(String xcxId,Integer appType) {
		HashMap<String, Object> hashMap = new HashMap<>();
		String sql1 = "select appid_model_id from huawu_xcx_model_state where appid_type="+appType+
				" order by id desc limit 1";
		Map<String, Object> select_item = huawuSystemMapper.select_item(sql1);
		if(select_item!=null){
			hashMap.putAll(select_item);
		}
		MfoyouThirdPartAudit selectByPrimaryKey = mfoyouThirdPartAuditMapper.selectByPrimaryKey(xcxId);
		if(selectByPrimaryKey!=null){
			hashMap.put("audit", selectByPrimaryKey);
			hashMap.put("versionId", selectByPrimaryKey.getVersionId()==null?0.1:Double.parseDouble(selectByPrimaryKey.getVersionId())+0.1);
		}else{
			MfoyouThirdPartAudit thirdPartAudit = new MfoyouThirdPartAudit();
			thirdPartAudit.setAppid(xcxId);
			hashMap.put("audit", thirdPartAudit);
			hashMap.put("versionId", 0.1);
			thirdPartAudit.setVersionId("0.1");
			mfoyouThirdPartAuditMapper.insertSelective(thirdPartAudit);
		}
		return hashMap;
	}

	@Override
	public String getAddressByappid(Integer appid) {
		String sql1 = "select address from huawu_xcx_model_state where appid_type="+appid+
				" order by id desc limit 1";
		Map<String, Object> select_item = huawuSystemMapper.select_item(sql1);
		if(select_item==null){
			return null;
		}
		String address = select_item.get("address").toString();
		return address;
	}
	
	@Override
	public List<Map<String, Object>> getWxinfo(Integer agentId) {
		String sql1 = "select  xcx_name,xcx_type,agent_id,state,version_id,currentModelId,appid "
				+ "from huawu_agent_wx_info  left join mfoyou_third_part_audit  on  "
				+ "huawu_agent_wx_info.xcx_id = mfoyou_third_part_audit.appid where  agent_id="+agentId;
		List<Map<String, Object>> select_list = huawuSystemMapper.select_list(sql1);
		return select_list;
	}
	
	/**
	 * 后期优化
	 */
	@Override
	public ArrayListObject<?> getWxinfoByXcxType(Integer apptype,Integer pageId,Integer pageSize, Integer userId, String key) {
		HuawuAgentWxInfoExample huawuAgentWxInfoExample = new HuawuAgentWxInfoExample();
		org.mfoyou.agent.common.dao.domain.HuawuAgentWxInfoExample.Criteria andXcxTypeEqualTo = huawuAgentWxInfoExample.or().andXcxTypeEqualTo(apptype);
		if(userId!=null){
			andXcxTypeEqualTo.andAgentIdEqualTo(userId);
		}
		if(key !=null ){
			andXcxTypeEqualTo.andXcxNameLick(key);
		}
		MybatisGenerator.setLimit(huawuAgentWxInfoExample, pageId, pageSize);
		int count = huawuAgentWxInfoMapper.countByExample(huawuAgentWxInfoExample);
		List<HuawuAgentWxInfo> selectByExample = huawuAgentWxInfoMapper.selectByExample(huawuAgentWxInfoExample);
		List<Integer> userIds = selectByExample.stream().map(HuawuAgentWxInfo::getAgentId).collect(Collectors.toList());
		HuawuUserExample huawuUserExample = new HuawuUserExample();
		huawuUserExample.or().andUserIdIn(userIds);
		List<HuawuUser> huawuUsers = huawuUserMapper.selectByExample(huawuUserExample);
		Map<Integer, HuawuUser> userPhone = huawuUsers.stream().collect(Collectors.toMap(HuawuUser::getUserId,f->f));
		List<String> xcxIds = selectByExample.stream().map(HuawuAgentWxInfo::getXcxId).collect(Collectors.toList());
		MfoyouThirdPartAuditExample mfoyouThirdPartAuditExample  = new MfoyouThirdPartAuditExample();
		mfoyouThirdPartAuditExample.or().andAppidIn(xcxIds);
		List<MfoyouThirdPartAudit> audits = mfoyouThirdPartAuditMapper.selectByExample(mfoyouThirdPartAuditExample);
		Map<String, MfoyouThirdPartAudit> auditInfos = audits.stream().collect(Collectors.toMap(MfoyouThirdPartAudit::getAppid,f->f));
		Function<HuawuAgentWxInfo,XcxRet> function = f -> {
			XcxRet xcxRet = new XcxRet();
			xcxRet.setAgentId(f.getAgentId());
			xcxRet.setXcxName(f.getXcxName());
			xcxRet.setAgentName(userPhone.get(f.getAgentId())==null?null:userPhone.get(f.getAgentId()).getUserNickName());
			xcxRet.setAgentAccount(userPhone.get(f.getAgentId())==null?null:userPhone.get(f.getAgentId()).getUserPhone());
			xcxRet.setModelId(auditInfos.get(f.getXcxId())==null?null:auditInfos.get(f.getXcxId()).getCurrentModelId());
			xcxRet.setState(auditInfos.get(f.getXcxId())==null?null:auditInfos.get(f.getXcxId()).getState());
			xcxRet.setVersion(auditInfos.get(f.getXcxId())==null?null:auditInfos.get(f.getXcxId()).getVersionId());
			return xcxRet;
		};
		List<XcxRet> xcxRets = selectByExample.stream().map(function).collect(Collectors.toList());
		return ArrayListObject.bliudArrayList(xcxRets, pageId, pageSize, count);
	}

	@Override
	public int addHuawuNotice(HuawuNotice huawuNotice) {
		int insertSelective = huawuNoticeMapper.insertSelective(huawuNotice);
		return insertSelective;
	}

	@Override
	public int updateNotice(HuawuNotice huawuNotice) {
		int updateRet = huawuNoticeMapper.updateByPrimaryKeySelective(huawuNotice);
		return updateRet;
	}
	

	@Override
	public ArrayListObject<?> getNotice(Integer pageId, Integer pageSize,int type) {
		HuawuNoticeExample huawuNoticeExample = new HuawuNoticeExample();
		huawuNoticeExample.or().andType(type);
		MybatisGenerator.setLimit(huawuNoticeExample, pageId, pageSize);
		huawuNoticeExample.setOrderByClause("id desc");
		List<HuawuNotice> huawuNotices = huawuNoticeMapper.selectByExampleWithBLOBs(huawuNoticeExample);
		int countByExample = huawuNoticeMapper.countByExample(huawuNoticeExample);
		return ArrayListObject.bliudArrayList(huawuNotices, pageId, pageSize, countByExample);
	}

	@Override
	public int deleteNotice(Integer id) {
		
		return huawuNoticeMapper.deleteByPrimaryKey(id);
	}

	@Override
	public HuawuNotice getSingleNotice(Integer id) {
		String sql1 = "select id from huawu_notice where id < "+id +" limit 0,1";
		String sql2 = "select id from huawu_notice where id > "+id +" limit 0,1";
		Map<String, Object> select_item1 = huawuSystemMapper.select_item(sql1);
		Map<String, Object> select_item2 = huawuSystemMapper.select_item(sql2);
		Integer preId = select_item1==null?null:Integer.valueOf(select_item1.get("id").toString());
		Integer nextId = select_item2==null?null:Integer.valueOf(select_item2.get("id").toString());
		HuawuNotice selectByPrimaryKey = huawuNoticeMapper.selectByPrimaryKey(id);
		if(selectByPrimaryKey==null)return null;
		selectByPrimaryKey.setPreId(preId);
		selectByPrimaryKey.setNextId(nextId);
		String sql3 = "update huawu_notice set touch_time = touch_time + 1";
		huawuSystemMapper.update_item(sql3);
		return selectByPrimaryKey;
	}

	@Override
	public int deleteEvaluate(Integer evaluateId,Integer agentId) {
		HuawuEvaluateExample huawuEvaluateExample = new HuawuEvaluateExample();
		huawuEvaluateExample.or().andAgentIdEqualTo(agentId).andEvaluateIdEqualTo(evaluateId);
		return huawuEvaluateMapper.deleteByExample(huawuEvaluateExample);
	}
	
	@Override
	public int deleteGoodsEvaluate(Integer evaluateId,Integer agentId) {
		HuawuGoodsEvaluateExample huawuGoodsEvaluateExample = new HuawuGoodsEvaluateExample();
		huawuGoodsEvaluateExample.or().andAgentIdEqualTo(agentId).andEvaluateIdEqualTo(evaluateId);
		return huawuGoodsEvaluateMapper.deleteByExample(huawuGoodsEvaluateExample);
	}
	
	@Override
	public int updateThirdPart(String appid,Integer state) {
		String sql  = "update mfoyou_third_part_audit set state = %d where appid = '%s' ";
		return huawuSystemMapper.update_item(String.format(sql, state,appid));
	}
}
